package com.hea7en.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hea7en.article.mapper.ArticleMapper;
import com.hea7en.article.service.ArticleService;
import com.hea7en.common.constants.ArticleConstants;
import com.hea7en.model.article.dto.ArticleHomeDto;
import com.hea7en.model.article.pojo.ApArticle;
import com.hea7en.model.common.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: hea7en
 * @Date: 2024/8/18 10:10 PM
 * @Description: 文章模块业务实现层
 */
@Slf4j
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService  {

    @Autowired
    ArticleMapper articleMapper;

    @Override
    public ResponseResult loadArticle(Short type, ArticleHomeDto articleHomeDto) {
       log.info("文章数据加载: type={}, articleHomeDto={}", type, articleHomeDto);

        // 校验参数
        if (!ArticleConstants.LOADTYPE_LOAD_MORE.equals(type)
                && !ArticleConstants.LOADTYPE_LOAD_NEW.equals(type)) {
            type = ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        // 分页size，默认给10条，最大50条
        Integer pageSize = articleHomeDto.getSize();
        if (pageSize==null || pageSize==0) {
            pageSize = 10;
        }
        Math.min(pageSize, ArticleConstants.PAGE_MAX_SIZE);
        // 频道ID，默认走推荐频道
        String tag = articleHomeDto.getTag();
        if (StringUtils.isBlank(tag)) {
            articleHomeDto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        // 最大时间，最小时间
        if (articleHomeDto.getMaxBehotTime()==null) {
            articleHomeDto.setMaxBehotTime(new Date());
        }
        if (articleHomeDto.getMinBehotTime()==null) {
            articleHomeDto.setMinBehotTime(new Date());
        }

        // 查询文章数据并返回
        List<ApArticle> articleList = articleMapper.loadArticleList(type, articleHomeDto);
        return ResponseResult.okResult(articleList);
    }
}
